package com.kiln.juxingqs.fragment

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DividerItemDecoration
import client.global.commlib.base.BaseFragment
import client.global.commlib.base.SyBannerAdapter
import client.global.commlib.comm_ui.draw.SpaceItemBottomWhiteDecoration
import client.global.commlib.comm_ui.draw.SpaceItemDecoration
import client.global.commlib.getScreenWidth
import client.global.commlib.utils.Util
import client.global.commlib.widget.EmptyLayout
import com.bumptech.glide.request.RequestOptions
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.ZrppAdapter
import com.kiln.juxingqs.adapter.ZrppOneNewAdapter
import com.kiln.juxingqs.api.bean.GoodsDetail
import com.kiln.juxingqs.api.bean.GoodsIndex
import com.kiln.juxingqs.databinding.FragmentTaboneNewBinding
import com.kiln.juxingqs.databinding.FragmentTabtwoBinding
import com.kiln.juxingqs.ui.LoginActivity
import com.kiln.juxingqs.ui.vm.MainViewModel
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.util.LogUtils

/**
 * 商城
 */
class TabOneNewFragment : BaseFragment<FragmentTaboneNewBinding, MainViewModel>() {

    override fun getViewId(): Int {
        return R.layout.fragment_tabone_new
    }

    val adapter: ZrppOneNewAdapter by lazy {
        ZrppOneNewAdapter(requireContext(),mImgLoader)
    }

    override fun initViewModel(): MainViewModel = createViewModel()

    override fun initWidget() {
        binding.recyclerView.adapter = adapter
        /*val decoration = DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL)
        val dividerDrawable = resources.getDrawable(R.drawable.drawable_divider)
        decoration.setDrawable(dividerDrawable)*/
        binding.recyclerView.addItemDecoration(SpaceItemDecoration(10))

        binding.statusBar.post {
            val h = Util.getStatusBarHeight(requireContext())
            val p = binding.statusBar.layoutParams
            p.height = h
            binding.statusBar.layoutParams = p
//            Util.setBg(resources.getColor(R.color.state_bg), requireActivity().window)
        }
        binding.refreshLayout.setOnRefreshListener {
            viewModel.storeList()
        }
        binding.emptyLayout.setOnClickListener {
            viewModel.storeList()
        }

        //获取数据
        viewModel.tradeCall.observe(this) {
            it?.let {
                if (it.isSuccess) {
                    adapter.hasMoreState.value = false
                    val list = it.data!!.goods_list as MutableList<GoodsIndex.Goods?>
                    val bannerList = it.data!!.banner_list as ArrayList<GoodsIndex.Banner>
                    bannerList?.let {
                        setBanner(bannerList)
                    }

                    if (list.size > 0) {
                        adapter.submitData(list, it.data!!)
                        adapter.hasMoreState.value = false
                    }
                    binding.emptyLayout.visibility = View.GONE
                } else {
                    if (adapter.itemCount == 0) {
                        binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                    }
                }
                binding.refreshLayout.isRefreshing = false
            }

        }
    }

    private fun setBanner(bannerList: ArrayList<GoodsIndex.Banner>) {
        if (bannerList != null && bannerList.size > 0) {
            val width = getScreenWidth(requireContext())
            val params: ViewGroup.LayoutParams = binding.banner.getLayoutParams()
            params.height = (width * 575 / 1400f).toInt()
            binding.banner.layoutParams = params
            binding.banner.indicator = CircleIndicator(requireContext())
            val list = bannerList
            binding.banner.setAdapter(object : SyBannerAdapter<GoodsIndex.Banner>(list) {
                override fun onBindView(
                    holder: BannerViewHolder?,
                    data: GoodsIndex.Banner?,
                    position: Int,
                    size: Int
                ) {
                    mImgLoader.load(data?.image_url)
                        .apply(RequestOptions().error(R.mipmap.ic_launcher)).into(
                            holder!!.imageView
                        )
                }
            })
            binding.banner.start()
        }
    }

    override fun onResume() {
        super.onResume()
        viewModel.storeList()
    }


    override fun initData() {

    }

}